package com.backend.controller;


import com.backend.service.AssAdminService;
import com.backend.service.UserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;

@RestController
@RequestMapping("/user")
@CrossOrigin(origins = "*")
public class UploadElectController {

    @Autowired
    AssAdminService assAdminService;

    @Autowired
    UserService userService;



    private static final Logger LOGGER = LoggerFactory.getLogger(UploadElectController.class);

    @PostMapping("/uploadStudentHeadImage/{Id}")
    String uploadStudentHeadImage(@RequestParam("file") MultipartFile multipartFile, @PathVariable("Id")String Id)throws Exception{

        if (multipartFile.isEmpty()) {
            return "请选择文件";
        }
        String fileName = multipartFile.getOriginalFilename();


        //更新社团的编号进行上传社团图片;

        Integer i=userService.uploadUserImages(fileName,Id);


        String filePath = "E:\\images";
        File dest = new File(new File(filePath).getAbsolutePath()+ "/" + fileName);
        dest.createNewFile();
        try {
            multipartFile.transferTo(dest);
            LOGGER.info("上传成功");
            return "上传成功";
        } catch (IOException e) {
            LOGGER.error(e.toString(), e);
        }
        return "上传失败！";
    }

    @PostMapping("/uploadElect/{Id}")
    String UploadElect(@RequestParam("file") MultipartFile multipartFile, @PathVariable("Id")String Id)throws Exception{

        if (multipartFile.isEmpty()) {
            return "请选择文件";
        }
        String fileName = multipartFile.getOriginalFilename();



        //更新社团的编号进行上传社团图片;

        Integer i=assAdminService.insertAssImages(fileName,Id);


        String filePath = "E:\\images";
        File dest = new File(new File(filePath).getAbsolutePath()+ "/" + fileName);
        dest.createNewFile();
        try {
            multipartFile.transferTo(dest);
            LOGGER.info("上传成功");
            return "上传成功";
        } catch (IOException e) {
            LOGGER.error(e.toString(), e);
        }
        return "上传失败！";
    }

    @PostMapping("/uploadActionImage/{Id}")
    String uploadActionImage(@RequestParam("file") MultipartFile multipartFile, @PathVariable("Id")String Id)throws Exception{

        if (multipartFile.isEmpty()) {
            return "请选择文件";
        }
        String fileName = multipartFile.getOriginalFilename();



        //更新社团的编号进行上传社团图片;

        Integer i=assAdminService.insertActionImage(Id,fileName);


        String filePath = "E:\\images";
        File dest = new File(new File(filePath).getAbsolutePath()+ "/" + fileName);
        dest.createNewFile();
        try {
            multipartFile.transferTo(dest);
            LOGGER.info("上传成功");
            return "上传成功";
        } catch (IOException e) {
            LOGGER.error(e.toString(), e);
        }
        return "上传失败！";
    }

//    社团注册时社团的图片
    @PostMapping("/uploadAssImage")
    String uploadAssImage(@RequestParam("file") MultipartFile multipartFile)throws Exception{

        if (multipartFile.isEmpty()) {
            return "请选择文件";
        }
        String fileName = multipartFile.getOriginalFilename();



        //更新社团的编号进行上传社团图片;


        String filePath = "E:\\images";
        File dest = new File(new File(filePath).getAbsolutePath()+ "/" + fileName);
        dest.createNewFile();
        try {
            multipartFile.transferTo(dest);
            LOGGER.info("上传成功");
            return "上传成功";
        } catch (IOException e) {
            LOGGER.error(e.toString(), e);
        }
        return "上传失败！";
    }
}

